var loadMoreView
// pages/live/live.js
const App = getApp()
// let AJAX = require("../../utils/util.js").AJAX
// let timeTranfer = require("../../utils/util.js").timeTranfer
let { AJAX, timeTranfer, imgUrl } = require("../../utils/util.js")
Page({

   /**
    * 页面的初始数据
    */ 
   data: { 
      navH: '',       
      page: 1,
      total: 0,
      listData: [], 
      isAuth: false, 
      // def:'https://img.alicdn.com/tps/i3/T1QYOyXqRaXXaY1rfd-32-32.gif',
      // show:false,
      floorstatus: false,
      text:'加载更多',
      showThis: false,    
      showIcon: false,
      isLoading: false,
     imgUrl: imgUrl
   },  

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      let vm =this
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         getApp().userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            vm.requestList()
            vm.requestAuch()
         }
         return
      }
      
    



      this.requestList()
      this.requestAuch()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() { 

   },

   /**
    * 生命周期函数--监听页面显示 
    */
   onShow: function() {
      this.setData({
         page:1
      })
      this.requestList()
      this.requestAuch()
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },
   // 获取滚动条当前位置
   onPageScroll: function (e) {     
      if (e.scrollTop > 200) {
         this.setData({
            floorstatus: true
         });
      } else {
         this.setData({
            floorstatus: false
         }); 
      }
   },
   goTop: function (e) {  // 一键回到顶部
      if (wx.pageScrollTo) {
         wx.pageScrollTo({
            scrollTop: 0
         })
      } else {
         wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
         })
      }
   },
   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {
    
      this.setData({
         page: 1
      })
      this.requestList()
      // 显示顶部刷新图标
      wx.showNavigationBarLoading();
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
      let vm = this

      if (vm.data.total > vm.data.listData.length) {
         vm.setData({
            page: vm.data.page + 1,
            showIcon:true,
            showThis: true          
         })
         vm.requestList(true)
      } else{
         this.setData({
            text:'我是有底线的',
            showIcon: false,
            showThis: true    
         })
      }
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   //图片点击事件
   imgYu: function(event) {
      var src = event.currentTarget.dataset.src; //获取data-src
      var imgList = event.currentTarget.dataset.list; //获取data-list
      //图片预览
      wx.previewImage({
         current: src, // 当前显示图片的http链接
         urls: imgList // 需要预览的图片http链接列表

      })

   },
   requestList(str) {
      let vm = this
    
         AJAX('broadcast/list', "GET", {
            type: 1,
            page: vm.data.page
         }).then(res => {
            // 隐藏导航栏加载框
            wx.hideNavigationBarLoading();
            // 停止下拉动作
            wx.stopPullDownRefresh();
            let _data = []
            if (str) {
               _data = vm.data.listData
             
            }
          
         
            res.data.forEach(function(item, index) {
               if (item.img) {
                  item.img = item.img.split(',')
               } else {
                  item.img = []
               }              
               _data.push(item)
            })
          
            vm.setData({
               total: res.header["X-Pagination-Total-Count"],
               listData: _data
            })
           

         })
     
   },
   // 点赞/取消
   zanClick(e) {
      let vm = this
      let _click = e.currentTarget.dataset.click
      let _id = e.currentTarget.dataset.id
      AJAX('broadcast/click', "POST", {
         broadcast_id: _id
      }).then(res => {
         vm.requestList().then(() => {
            wx.showToast({
               title: !_click 
            })
         })
      })
   },
   // 评论
   messageClick(e) {
      let vm = this
      let _id = e.currentTarget.dataset.id
      let _toid = e.currentTarget.dataset.toid
      if (!vm.data.isAuth) {
         wx.navigateTo({
            url: '/pages/authorization/authorization',
         })
         return
      }

      this.setData({
         messageid: _id,
         focus: true,
         toid: _toid
      })
   },
   sendMessage(e) {
      let _val = e.detail.value
      let vm = this
      AJAX('broadcast/send-msg', 'POST', {
         broadcast_id: vm.data.messageid,
         content: _val,
         to_id: vm.data.toid || ''
      }).then(res => {
         wx.showToast({
            title: "评论成功",
         })
         vm.setData({
            messageid: '',
            focus: false,
            to_id: ''
         })
         vm.requestList()
      })
   },
   navBack() {
      let vm = this
      if (!vm.data.isAuth) {
         return wx.navigateTo({
            url: '/pages/authorization/authorization',
         })
      }
      wx.navigateTo({
         url: '/pages/issue/issue',
      })
   },
   requestAuch() {
      let vm = this
      AJAX("member/auth").then(res => {
         vm.setData({
            isAuth: res.data.auth
         })
      })
   },
   // 展示图片
   showImg(e) {
      let _list = e.currentTarget.dataset.list
      let _url = e.currentTarget.dataset.index
      wx.previewImage({
         urls: _list,
         current: _url
      })
   },
   //删除
   delBroadcast(e) {
      let id = e.currentTarget.dataset.id
      AJAX('broadcast/del', 'GET', {
         id: id
      }).then(res => {
         if (res.status = "succeed") {
            wx.showToast({
               title: '删除成功',
            })
            this.requestList()
         } else {
            wx.showToast({
               icon: none,
               title: '删除失败',
            })
         }
         console.log(res);
      })
   }
})